﻿using System;
using EWSEditor.Forms;
using System.Windows.Forms;

namespace EWSEditor.CRM
{
    class CRMMenuOptionsHelper
    {
        public static void EnableCRMMenuOptions(MenuStrip menu, CRMMenuMode menumode)
        {
           foreach (ToolStripMenuItem menuitem in menu.Items)
            {
                if (menuitem.Text == "File")
                {
                    foreach (ToolStripDropDownItem dropdown in menuitem.DropDownItems)
                    {
                        if (dropdown.Text == "New Dynamics 365 Service...")
                        {
                            dropdown.Enabled = false;
                            break;
                        }
                    }
                }

                if (menuitem.Text == "Dynamics 365")
                {
                    EnableCommonMenuOptions(menuitem);

                    if (menumode == CRMMenuMode.QueueMode)
                    {
                        foreach (var dropdown in menuitem.DropDownItems)
                        {
                            var menuItem = dropdown as ToolStripDropDownItem;
                            if (menuItem != null)
                            {
                                if (menuItem.Name == "mnuSyncEmulators") { menuItem.Enabled = true; continue; }
                                if (menuItem.Name == "mnuEmailValidator") { menuItem.Enabled = true; continue; }
                                //if (menuItem.Name == "mnuAutodiscoverUtility") { menuItem.Enabled = true; continue; }
                            }
                        }
                    }

                    if (menumode == CRMMenuMode.UserMode)
                    {
                        foreach (var dropdown in menuitem.DropDownItems)
                        {
                            var menuItem = dropdown as ToolStripDropDownItem;
                            if (menuItem != null)
                            {
                                if (menuItem.Name == "mnuSyncEmulators") { menuItem.Enabled = true; continue; }
                                if (menuItem.Name == "mnuEmailValidator") { menuItem.Enabled = true; continue; }
                                if (menuItem.Name == "mnuLastSyncDetails") { menuItem.Enabled = true; continue; }
                                if (menuItem.Name == "mnuExchangeSyncTroubleshooter") { menuItem.Enabled = true; continue; }
                                //if (menuItem.Name == "mnuAutodiscoverUtility") { menuItem.Enabled = true; continue; }

                                if (CrmOrgMailboxCache.UserOwnsTargetMailbox)
                                {
                                    if (menuItem.Name == "mnuViewMappings") { menuItem.Enabled = true; continue; }
                                    if (menuItem.Name == "mnuDynamicsSyncEmulator") { menuItem.Enabled = true; continue; }
                                }
                            }
                        }
                    }

                    if (menumode == CRMMenuMode.SupportMode)
                    {
                        foreach (var dropdown in menuitem.DropDownItems)
                        {
                            var menuItem = dropdown as ToolStripDropDownItem;
                            if (menuItem != null)
                            {
                                if ((menuItem.Name == "mnuLastSyncDetails") && (!CrmOrgMailboxCache.TargetMailboxIsQueue)) { menuItem.Enabled = true; continue; }
                                //if (menuItem.Name == "mnuAutodiscoverUtility") { menuItem.Enabled = false; continue; }
                            }
                        }
                    }
                }
                if (menuitem.Text == "Support Mode")
                {
                    if (menumode == CRMMenuMode.SupportMode)
                    {
                        menuitem.Visible = true;
                    }
                }
            }
        }

        private static void EnableCommonMenuOptions(ToolStripMenuItem menuitem)
        {
            foreach(var dropDownItem in menuitem.DropDownItems)
            {
                var menuItem = dropDownItem as ToolStripDropDownItem;
                if (menuItem != null)
                {
                    if (menuItem.Name == "mnuCRMDiagnostic") { menuItem.Enabled = true; continue; }
                    if (menuItem.Name == "mnuMailboxDetails") { menuItem.Enabled = true; continue; }
                    if (menuItem.Name == "mnuConfigurationDetails") { menuItem.Enabled = true; continue; }
                }
                
            }
        }

        /// <summary>
        /// Initializes Dynamics 365 Menu Options that are only dependent on an Active Exchange Service.
        /// </summary>
        /// <param name="menu"></param>
        internal static void EnableExchangeServiceDependentOptions(MenuStrip menu)
        {
            foreach (ToolStripMenuItem menuitem in menu.Items)
            {
                if (menuitem.Text == "Dynamics 365")
                {
                    foreach (var dropdown in menuitem.DropDownItems)
                    {
                        var menuItem = dropdown as ToolStripDropDownItem;
                        if (menuItem != null)
                        {
                            if (menuItem.Name == "mnuAutodiscoverUtility")
                            {
                                menuItem.Enabled = true;
                                break;
                            }
                        }
                    }
                }
            }

         }

        internal static void ResetMenuState(MenuStrip menu)
        {
            foreach (ToolStripMenuItem menuitem in menu.Items)
            {
                if (menuitem.Text == "Dynamics 365")
                {
                    foreach (var dropdown in menuitem.DropDownItems)
                    {
                        var menuItem = dropdown as ToolStripDropDownItem;
                        if (menuItem != null)
                        {
                            menuItem.Enabled = false;
                        }
                    }
                }
                if (menuitem.Text == "File")
                {
                    foreach (var dropdown in menuitem.DropDownItems)
                    {
                        var menuItem = dropdown as ToolStripDropDownItem;
                        if (menuItem.Name == "mnuCrmService")
                        {
                            menuItem.Enabled = false;
                            break;
                        }
                    }
                }

            }
        }
    }
}

         
          
        
      